<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: Render to MSAA Renderbuffer</title>
    <meta charset="utf-8">
    <script src="utils/gl-matrix.js"></script>
    <script src="../build/picogl.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: Render to MSAA Renderbuffer
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/rtt_msaa.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es
        
        layout(std140, column_major) uniform;

        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        layout(location=2) in vec4 normal;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform mat4 uModel;
        
        out vec3 vPosition;
        out vec2 vUV;
        out vec3 vNormal;
        void main() {
            vec4 worldPosition = uModel * position;
            vPosition = worldPosition.xyz;
            vUV = uv;
            vNormal = (uModel * normal).xyz;
            gl_Position = uViewProj * worldPosition;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;

        layout(std140, column_major) uniform;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform sampler2D tex;
        
        in vec3 vPosition;
        in vec2 vUV;
        in vec3 vNormal;

        out vec4 fragColor;
        void main() {
            vec3 color = texture(tex, vUV).rgb;

            vec3 normal = normalize(vNormal);
            vec3 eyeVec = normalize(uEyePosition.xyz - vPosition);
            vec3 incidentVec = normalize(vPosition - uLightPosition.xyz);
            vec3 lightVec = -incidentVec;
            float diffuse = max(dot(lightVec, normal), 0.0);
            float highlight = pow(max(dot(eyeVec, reflect(incidentVec, normal)), 0.0), 100.0);
            float ambient = 0.1;
            fragColor = vec4(color * (diffuse + highlight + ambient), 1.0);
        }
    </script>
    <script type="text/javascript">
        utils.addTimerElement();

        var canvas = document.getElementById("gl-canvas");

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .depthTest();

        var timer = app.createTimer();

        // PROGRAM
        var vsSource =  document.getElementById("vertex-draw").text.trim();
        var fsSource =  document.getElementById("fragment-draw").text.trim();
        var program = app.createProgram(vsSource, fsSource);

        // FRAMEBUFFERS

        // Use maxium number of available samples.
        var colorTarget = app.createRenderbuffer(app.width, app.height, PicoGL.RGBA8, PicoGL.WEBGL_INFO.SAMPLES);
        var depthTarget = app.createRenderbuffer(app.width, app.height, PicoGL.DEPTH_COMPONENT16, PicoGL.WEBGL_INFO.SAMPLES);
        var msaaFramebuffer = app.createFramebuffer().colorTarget(0, colorTarget).depthTarget(depthTarget);

        var textureColorTarget = app.createTexture2D(app.width, app.height);
        var textureFramebuffer = app.createFramebuffer().colorTarget(0, textureColorTarget);


        // GEOMETRY
        var box = utils.createBox({dimensions: [1.0, 1.0, 1.0]})
        var positions = app.createVertexBuffer(PicoGL.FLOAT, 3, box.positions);
        var uv = app.createVertexBuffer(PicoGL.FLOAT, 2, box.uvs);
        var normals = app.createVertexBuffer(PicoGL.FLOAT, 3, box.normals);

        var boxArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions)
        .vertexAttributeBuffer(1, uv)
        .vertexAttributeBuffer(2, normals);

        // UNIFORMS
        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(1, 1, 1);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var viewProjMatrix = mat4.create();
        mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 0.5);

        // UNIFORM BUFFER
        var sceneUniformBuffer = app.createUniformBuffer([
            PicoGL.FLOAT_MAT4,
            PicoGL.FLOAT_VEC4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, viewProjMatrix)
        .set(1, eyePosition)
        .set(2, lightPosition)
        .update();

        var modelMatrix = mat4.create();
        var rotateXMatrix = mat4.create();
        var rotateYMatrix = mat4.create();

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);
            msaaFramebuffer.resize();
            textureFramebuffer.resize();

            mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 10.0);
            mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

            sceneUniformBuffer.set(0, viewProjMatrix).update();
        };

        
        utils.loadImages(["img/webgl-logo.png"], function(images) {
            // CREATE TEXTURE
            var texture = app.createTexture2D(images[0], { flipY: true });

            var drawCall = app.createDrawCall(program, boxArray)
            .uniformBlock("SceneUniforms", sceneUniformBuffer);

            var angleX = 0;
            var angleY = 0;

            function draw() {
                if (timer.ready()) {
                    utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
                }

                timer.start();

                angleX += 0.01;
                angleY += 0.02;

                mat4.fromXRotation(rotateXMatrix, angleX);
                mat4.fromYRotation(rotateYMatrix, angleY);
                mat4.multiply(modelMatrix, rotateXMatrix, rotateYMatrix);

                drawCall.uniform("uModel", modelMatrix);

                // RENDER TO OFFSCREEN TEXTURE
                app.drawFramebuffer(msaaFramebuffer).clearColor(0.4, 0.4, 0.4, 1.0).clear();
                drawCall.texture("tex", texture).draw();
                
                // Can't sample from renderbuffer so blit to a texture for sampling.
                app.readFramebuffer(msaaFramebuffer)
                .drawFramebuffer(textureFramebuffer)
                .blitFramebuffer(PicoGL.COLOR_BUFFER_BIT);

                // RENDER TO SCREEN
                app.defaultDrawFramebuffer().clearColor(0.0, 0.0, 0.0, 1.0).clear()
                drawCall.texture("tex", textureFramebuffer.colorAttachments[0]).draw();

                timer.end(); 

                requestAnimationFrame(draw);
            }

            requestAnimationFrame(draw);
            
        });

    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>
